<?

$calendars_RET = DBGet(DBQuery("SELECT min(SCHOOL_DATE) AS MIN,max(SCHOOL_DATE) AS MAX,SCHOOL_ID,SYEAR FROM ATTENDANCE_CALENDAR GROUP BY SCHOOL_ID,SYEAR"));
foreach($calendars_RET as $dates)
{
	DBQuery("INSERT INTO SCHOOL_YEARS (MARKING_PERIOD_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,START_DATE,END_DATE,YEAR_FRACTION) values(nextval('MARKING_PERIOD_SEQ'),'".$dates['SYEAR']."','".$dates['SCHOOL_ID']."','".$dates['SYEAR'].'-'.($dates['SYEAR']+1)."','".$dates['SYEAR']."','".$dates['MIN']."','".$dates['MAX']."','1')");
	DBQuery("UPDATE SCHOOL_SEMESTERS SET YEAR_ID=currval('MARKING_PERIOD_SEQ') WHERE SYEAR='".$dates['SYEAR']."' AND SCHOOL_ID='".$dates['SCHOOL_ID']."'");
}

$semesters_count = DBGet(DBQuery("SELECT SYEAR,COUNT(*) AS COUNT FROM SCHOOL_SEMESTERS GROUP BY SYEAR"));
foreach($semesters_count as $syear)
	DBQuery("UPDATE SCHOOL_SEMESTERS SET YEAR_FRACTION='".(1/$syear['COUNT'])."' WHERE SYEAR='".$syear['SYEAR']."'");

$quarters_count = DBGet(DBQuery("SELECT SYEAR,COUNT(*) FROM SCHOOL_QUARTERS GROUP BY SYEAR"));
foreach($quarters_count as $syear)
	DBQuery("UPDATE SCHOOL_QUARTERS SET YEAR_FRACTION='".(1/$syear['COUNT'])."' WHERE SYEAR='".$syear['SYEAR']."'");

?>
